#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<Qpainter>
#include<QTimer>
#include<QKeyEvent>
#include<QSound>
#include<QString>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),blsrun(false),Speed(500)
{
    ui->setupUi(this);
    this->setGeometry(QRect(10,50,1300,1300));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    if(!blsrun)
    huashe();
    //hua bei jing
    painter.setPen(Qt::black);//hua bi yan se
    painter.setBrush(Qt::darkGray);//tian chong yan se
    painter.drawRect(10,20,1020,1030);

    painter.setPen(Qt::black);//hua bi yan se
    painter.setBrush(Qt::white);//tian chong yan se
    painter.drawRect(20,25,1000,1020);
    painter.drawPixmap(20,25,1000,1020,QPixmap(":/tu/背景.jpg"));


    //xian shi kai shi jie shu !!!!
    QFont font1("Courier",24);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(200,200,display);

    //fen shu
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(900,1075,"score:");
    painter.drawText(1000,1075,QString::number(fen));
    //cao zuo ti shi
    QFont fon3("Courier",24);
    painter.setFont(fon3);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(100,1075,"水娃： W A S D控制");
    painter.drawText(100,1105,"火娃：上下左右键控制");
    painter.drawText(400,1075,"颜色对应+20分，速度较大提升");
    painter.drawText(400,1105,"颜色不对应+10分，速度小幅提升");
    painter.drawText(400,1135,"吃到绿色钻石-20分，速度下降");


    //hua she
    painter.drawRect(apple);
    painter.drawPixmap(apple,QPixmap(":/tu/蓝钻.jpg"));
    //2
    painter.drawRect(apple2);
    painter.drawPixmap(apple2,QPixmap(":/tu/红钻.jpg"));
    //3
    painter.drawRect(apple3);
    painter.drawPixmap(apple3,QPixmap(":/tu/绿钻.jpg"));
  //  painter.drawRect(sheshen);
    painter.drawPixmap(sheshen[0],QPixmap(":/tu/火娃.jpg"));
    for(int i=1;i<sheshen.size();i++)
    {
        painter.setPen(Qt::black);
        painter.setBrush(Qt::darkRed);
        painter.drawRect(sheshen[i]);
    }
    painter.drawPixmap(sheshen2[0],QPixmap(":/tu/水娃.jpg"));
    for(int i=1;i<sheshen2.size();i++)
       {
        painter.setPen(Qt::black);
        painter.setBrush(Qt::darkBlue);
        painter.drawRect(sheshen2[i]);
    }
    if(blsend)
        timer->stop();
}
void MainWindow::huashe(){
    blsrun=true;
    blsend=false;
    fangxiang=2;
    fangxiang2=2;
    display="game start";
    fen=0;
    apple=applecreate();
    apple2=applecreate2();
    apple3=applecreate3();
    //QRect rect(100,70,10,10);
    //sheshen=rect;
    sheshen.resize(5);//she chang
    sheshen2.resize(5);//she chang
    for(int i=0;i<sheshen.size();i++)
    {
        QRect rect(100,60+20*i,20,20);
        sheshen[sheshen.size()-1-i]=rect;
    }
    for(int i=0;i<sheshen2.size();i++)
    {
        QRect rect(100,600+20*i,20,20);
        sheshen2[sheshen2.size()-1-i]=rect;
    }
    timer=new QTimer(this);//ji shi qi shi jian
    timer->start(Speed);//shua xin shi jian
    connect(timer,SIGNAL(timeout()),SLOT(gengxin()));

}
void MainWindow::gengxin(){
    display=" ";
    shetou=sheshen.first();//she tou
    shetou2=sheshen2.first();//she tou
    eat();
    ziji();
    peng();
    yinyue();
    for(int j=0;j<sheshen.size()-1;j++)
        sheshen[sheshen.size()-1-j]=sheshen[sheshen.size()-2-j];
    for(int j=0;j<sheshen2.size()-1;j++)
        sheshen2[sheshen2.size()-1-j]=sheshen2[sheshen2.size()-2-j];
    switch(fangxiang) {
    case 1:
        shetou.setTop(shetou.top()-20);
        shetou.setBottom(shetou.bottom()-20);
        break;
    case 2:
        shetou.setTop(shetou.top()+20);
        shetou.setBottom(shetou.bottom()+20);
        break;
    case 3:
        shetou.setLeft(shetou.left()-20);
        shetou.setRight(shetou.right()-20);
        break;
    case 4:
        shetou.setLeft(shetou.left()+20);
        shetou.setRight(shetou.right()+20);
        break;
    default:;
    }
    switch(fangxiang2) {
    case 1:
        shetou2.setTop(shetou2.top()-20);
        shetou2.setBottom(shetou2.bottom()-20);
        break;
    case 2:
        shetou2.setTop(shetou2.top()+20);
        shetou2.setBottom(shetou2.bottom()+20);
        break;
    case 3:
        shetou2.setLeft(shetou2.left()-20);
        shetou2.setRight(shetou2.right()-20);
        break;
    case 4:
        shetou2.setLeft(shetou2.left()+20);
        shetou2.setRight(shetou2.right()+20);
        break;
    default:;
    }
    sheshen[0]=shetou;
    sheshen2[0]=shetou2;
    if(shetou.left()<15||shetou.right()>1050||shetou.top()<25||shetou.bottom()>1030)
    {
        blsend=true;
        display="game over";
    }
    if(shetou2.left()<15||shetou2.right()>1050||shetou2.top()<25||shetou2.bottom()>1030)
    {
        blsend=true;
        display="game over";
    }
    update();//geng xin
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*) event;
    switch(key->key()){
    case Qt::Key_Up:fangxiang=1;
        break;
    case Qt::Key_Down:fangxiang=2;
        break;
    case Qt::Key_Left:fangxiang=3;
        break;
    case Qt::Key_Right:fangxiang=4;
        break;
    default:;
    }
    QKeyEvent *key2=(QKeyEvent*) event;
    switch(key2->key()){
    case Qt::Key_W:fangxiang2=1;
        break;
    case Qt::Key_S:fangxiang2=2;
        break;
    case Qt::Key_A:fangxiang2=3;
        break;
    case Qt::Key_D:fangxiang2=4;
        break;
    default:;
    }
}
QRect MainWindow::applecreate()
{
    int x,y;
    x=qrand()%25;
    y=qrand()%25;//!!!!!!!!!!!!!!!!!!!!!!
    QRect rect(20+x*20,40+y*20,20,20);
    return rect;
}
QRect MainWindow::applecreate2()
{
    int x,y;
    x=qrand()%25;
    y=qrand()%25;//!!!!!!!!!!!!!!!!!!!!!!
    QRect rect(40+x*20,200+y*20,20,20);
    return rect;
}
QRect MainWindow::applecreate3()
{
    int x,y;
    x=qrand()%25;
    y=qrand()%25;//!!!!!!!!!!!!!!!!!!!!!!
    QRect rect(40+x*20,100+y*20,20,20);
    return rect;
}
void MainWindow::eat()
{
    if(shetou==apple)
    {
        shetou=apple;
        sheshen.push_back(sheshen.last());
        apple=applecreate();
        fen=fen+10;
        if(Speed<=0) Speed=1;
        if(Speed>0 )Speed=Speed-10;
        timer->stop();//ting zhi
        timer->start(Speed);
    }
    if(shetou2==apple2)
    {
        shetou2=apple2;
        sheshen2.push_back(sheshen2.last());
        apple2=applecreate2();
        fen=fen+10;
        if(Speed<=10) Speed=1;
        if(Speed>10 )Speed=Speed-10;
        timer->stop();//ting zhi
        timer->start(Speed);
    }
    if(shetou==apple2)
    {
        shetou=apple2;
        sheshen.push_back(sheshen.last());
        apple2=applecreate2();
        fen=fen+20;
        if(Speed<=30) Speed=1;
        if(Speed>30 )Speed=Speed-30;
        timer->stop();//ting zhi
        timer->start(Speed);
    } if(shetou2==apple)
    {
        shetou2=apple;
        sheshen2.push_back(sheshen2.last());
        apple=applecreate();
        fen=fen+20;
        if(Speed<=10) Speed=1;
        if(Speed>10 )Speed=Speed-10;
        timer->stop();//ting zhi
        timer->start(Speed);
    }
    if(shetou==apple3)
    {
        shetou=apple3;
        sheshen.push_back(sheshen.last());
        apple3=applecreate3();
        fen=fen-20;
        Speed=Speed+50;
        timer->stop();//ting zhi
        timer->start(Speed);
    }
    if(shetou2==apple3)
    {
        shetou2=apple3;
        sheshen2.push_back(sheshen2.last());
        apple3=applecreate3();
        fen=fen-20;
        Speed=Speed+50;
        timer->stop();//ting zhi
        timer->start(Speed);
    }
}
void MainWindow::ziji()
{
    for(int i=1;i<sheshen.size();i++)
    {
        if(shetou==sheshen[i])
        {
            display="game over";
            blsend=true;
            update();
        }
    }
    for(int i=1;i<sheshen2.size();i++)
    {
        if(shetou2==sheshen2[i])
        {
            display="game over";
            blsend=true;
            update();
        }
    }
}
void MainWindow::peng()
{
    for(int i=0;i<sheshen.size();i++)
    {
        for(int j=0;j<sheshen2.size();j++)
        {
        if(sheshen[i]==sheshen2[j])
        {
            display="game over";
            blsend=true;
            update();
        }
        }
    }
}
void MainWindow::yinyue ()
{
    if(a==0||a==100||a==200||a==300||a==400)
    {
        QSound::play(":/ge/beijing.wav");
    }
    a=a+1;
}
